package com.example.drzhang.oengldemo.render;

import android.content.Context;
import android.opengl.GLES20;

import com.example.drzhang.oengldemo.R;

/**
 * Created by drzhang on 2018/1/10.
 */

public class TextureShaderProgram extends ShaderProgram {

    private int uMatrixLocation;
    private int uTextureUnitLocation;

    private int aPosition;
    private int aTextureCoordinatesLocation;


    protected TextureShaderProgram(Context context) {
        super(context, R.raw.texture_vertex_shader, R.raw.texture_fragment_shader);
        uMatrixLocation = GLES20.glGetUniformLocation(program, U_MATRIX);
        uTextureUnitLocation = GLES20.glGetUniformLocation(program, U_TEXTURE_UNIT);

        aPosition = GLES20.glGetAttribLocation(program, A_POSITION);
        aTextureCoordinatesLocation = GLES20.glGetAttribLocation(program, A_TEXTURE_COORDINATES);
    }

    public void setUniforms(float[] matrix, int textureId){
        GLES20.glUniformMatrix4fv(uMatrixLocation,1,false,matrix,0);
        //设置活动的纹理单元为 纹理单元0
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        //将纹理绑定到纹理单元0 上面
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        //将纹理单元0 传递给着色器对象
        GLES20.glUniform1i(uTextureUnitLocation, 0);
    }

    public int getPositionAttributeLocation(){
        return aPosition;
    }

    public int getTextureCoordinatesAttributeLocation(){
        return aTextureCoordinatesLocation;
    }

}
